﻿using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour {
	private float timer=0;
	private const float TIMER=1f;
	private Player player;
	public GameObject spawnUnit;
	void Start () {
		player = (Player)transform.parent.gameObject.GetComponent("Player");
	}
	void Update () {
		timer+=Time.deltaTime;
		if(timer>TIMER){
			timer=0;
			if(player.getPopLeft()>0){
				Vector3 v = Random.insideUnitCircle;
				v.Normalize();
				v*=8;
				v.z=v.y;
				v.y=0;
				GameObject newUnit=(GameObject)Instantiate(spawnUnit,transform.position+v,spawnUnit.transform.rotation);
				Unit u = (Unit)newUnit.GetComponent("Unit");
				u.attackDamage+= player.attackUpgrade;			//*2.5f;
				u.attackSpeed*= (2.5f-player.atkSpeedUpgrade*.05f);
				u.health+= player.lifeUpgrade*20;				//10;
				u.speed+= player.speedUpgrade;					//*2;
				u.obj.GetComponent<NavMeshAgent>().speed=u.speed;
				u.goldValue+= (player.attackUpgrade+player.atkSpeedUpgrade+player.lifeUpgrade+player.speedUpgrade)*3;
				player.addUnit(u);
				u.owner=player;
				if(u.obj.GetComponent<ParticleSystem>()!=null)
					u.obj.GetComponent<ParticleSystem>().Play(false);
				if(player==Player.Controller){
					u.obj.renderer.enabled=true;

				}
				player.spawnGroup.myUnits.Add(u);
				u.myGroup=player.spawnGroup;
				u.myGroup.target=player.commandCenter.obj.transform.position;
				GroupManager man = (GroupManager)GameObject.Find("Managers").GetComponent("GroupManager");
				if(!man.groups.Contains(u.myGroup))
					man.groups.Add(u.myGroup);
			}
		}
	}
	
	Vector3 findSpace(){
		return new Vector3(0,0,0);
	}
	
}
